import {Injectable} from '@angular/core';
import {BarnProject} from "../entity/barn-project";
import {Observable} from "rxjs";
import {ResponseEntity} from "../../../../../services/http/response-entity";
import {HttpUtilService} from "../../../../../services/http/http-util.service";
import {environment} from "../../../../../../environments/environment";
import {RoleUser} from "../../../basic-data/role/entity/role-user";
import {CloneProjectDto} from "../entity/clone-project-dto";

@Injectable({
  providedIn: 'root'
})
export class ProjectService {

  private prefix = environment.serverName.pp + "/project/"
  private pu_prefix = environment.serverName.pp + "/project-user/"

  constructor(private http: HttpUtilService) {
  }

  query(param: BarnProject, current = 0, pageSize = 0, handleError = true): Observable<ResponseEntity<BarnProject[]>> {
    return this.http.getPage(this.prefix + "query", current, pageSize, handleError, param);
  }

  getById(pid: string, handleError = true): Observable<BarnProject> {
    return this.http.get(this.prefix + "getById/" + pid, handleError)
  }

  save(param: BarnProject, handleError = true): Observable<BarnProject> {
    return this.http.post(this.prefix + "save", param, handleError)
  }

  getUsers(projectId: string, handleError = true): Observable<RoleUser[]> {
    return this.http.get(this.pu_prefix + "getUsers/" + projectId, handleError)

  }

  addUsers(projectId: string, userIds: string[], handleError = true) {
    return this.http.post(this.pu_prefix + "addUsers", {projectId, userIds}, handleError)
  }

  deleteUser(projectId: string, userId: string, handleError = true) {
    return this.http.post(this.pu_prefix + "deleteUser", {projectId, userId}, handleError)
  }

  mine(param: BarnProject, handleError = true): Observable<BarnProject[]> {
    return this.http.get(this.prefix + "mine", handleError, param)
  }

  clone(param: CloneProjectDto, handleError = true): Observable<void> {
    return this.http.post(this.prefix + "cloneProject", param, handleError);
  }


}
